<template>
	<jk-dialog
		v-model="showDialog"
		:title="`选择-${title}`"
		width="1000px"
		append-to-body
		@on-visible-change="onVisibleChange"
		@on-cancel="onVisibleChange(false)"
		@on-confirm="onConfirmBtn"
	>
		<header ref="box1" class="margin-bottom-4 flex-between-center">
			<div>
				<el-radio-group v-model="searchParams.auditState" size="small" class="item-gap" @change="onStateChange">
					<el-radio-button v-for="item in noticeStateList" :key="item.id" :label="item.id">
						<el-badge v-if="item.id <= 3 && item.count > 0" :value="item.count" :max="99">
							<span class="margin-right-20">{{ item.name }}</span>
						</el-badge>
						<div v-else class="margin-right-10" style="padding-bottom: 2px;">{{ item.name }}</div>
					</el-radio-button>
				</el-radio-group>
			</div>
			<div class="flex-start-center">
				<jk-search-input
					v-model="searchParams.productName"
					class="search-bar-item-size3 item-gap"
					placeholder="品种编号或名称"
					@on-enter="onSearchBtn"
				></jk-search-input>
				<jk-search-input
					v-model="searchParams.batchCode"
					class="search-bar-item-size2 item-gap"
					placeholder="批号"
					@on-enter="onSearchBtn"
				></jk-search-input>
				<jk-search-input
					v-model="searchParams.code"
					class="search-bar-item-size2 item-gap"
					placeholder="单据编号"
					@on-enter="onSearchBtn"
				></jk-search-input>
				<jk-search-button @click="onSearchBtn"></jk-search-button>
			</div>
		</header>

		<jk-table
			ref="jkTable"
			:radio-config="{trigger: 'row'}"
			:loading="tableLoading"
			class="width-100-percent"
			:data="tableData"
			border
			highlight-hover-row
			highlight-current-row
			:height="tableHeight - 100"
			@radio-change="onRadioChange"
			@cell-dblclick="onCellDblclick"
		>
			<vxe-table-column type="radio" width="60" align="center" title="单选" fixed="left"></vxe-table-column>
			<vxe-table-column field="code" title="单据编号" :width="150" />
			<vxe-table-column field="batchCode" title="批号" :min-width="100" />
			<vxe-table-column field="productName" title="品种名称" fixed :min-width="200"></vxe-table-column>
			<vxe-table-column field="packQty" align="right" title="打包数量" :width="100" />
			<vxe-table-column field="productOrderCode" title="生产订单号" :width="150" />
		</jk-table>
		<footer ref="box2" class="flex-end-center margin-top-10">
			<jk-page
				:page-index="searchParams.pageNo"
				:page-sizes="searchParams.pageSizes"
				:page-size="searchParams.pageSize"
				:total="totalCount"
				layout="total, prev, pager, next"
				@on-change="onPageChange"
			/>
		</footer>
	</jk-dialog>
</template>

<script>
    import calDialogHeight from '../../../../../mixins/cal-table-height';
    import { pageConfig } from '../../../../../utils/jk/jk-tool';
    import { productionChangeProductRecordPage } from '../../../../../api/production/xsNotice';
    import { noticeStateList } from '../../../../../utils/jk/status-option-list';

    export default {
        mixins: [calDialogHeight],
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            title: {
                type: String,
                default: ' 改包装通知单'
            },
            processCode: {
                type: String,
                default: 'XS'
            }
        },
        data() {
            return {
                totalCount: 0,
                workshopList: [],
                noticeStateList: [
                    { id: 0, name: '待下达', color: '#808695' },
                    { id: 1, name: '待开台', color: '#ff9900' },
                    { id: 2, name: '生产中', color: '#2b85e4' }
                ],
                searchParams: {
                    code: '',
                    pageNo: pageConfig.pageNo,
                    auditState: 1,
                    productName: '',
                    pageSize: pageConfig.pageSize,
                    nameOrCode: '',
                    type: 0,
                    name: ''
                },
                tableData: [],
                showDialog: false,
                tableLoading: false,
                selectData: []
            };
        },
        methods: {
            onStateChange() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onCellDblclick(e) {
                this.$emit('on-confirm', e.row);
            },
            onRadioChange(selection) {
                this.selectData = selection.row;
            },
            checkboxMethod({ row }) {
                return row._disabled !== true;
            },
            onSearchBtn() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onPageChange(e, size) {
                this.$set(this.searchParams, 'pageNo', e);
                this.$set(this.searchParams, 'pageSize', size);
                this.getListRequest();
            },
            onConfirmBtn() {
                if (Object.keys(this.selectData).length) {
                    this.$emit('on-confirm', this.selectData);
                } else {
                    this.$tipsBar(this, 'unChoose');
                }
            },
            getListRequest: function() {
                this.tableLoading = true;
                this.selectData = [];
                return productionChangeProductRecordPage(this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.totalCount = res.data.count;
                        this.tableData = res.data.res;
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                }
            },
            async getDependentData() {
                this.tableLoading = true;
                this.workshopList = this.$store.state.user.userWorkshop.workshopList || [];
                await this.getListRequest();
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
